﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace TouchExplorer
{
    /// <summary>
    /// Interaction logic for PathView.xaml
    /// </summary>
    public partial class PathView : WrapPanel
    {
        private ExplorerViewModel Model
        {
            get
            {
                return DataContext as ExplorerViewModel;
            }
        }

        public PathView()
        {
            InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(PathView_DataContextChanged);
        }

        void PathView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.InitEvents();
        }

        void CurrentPath_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Children.Clear();
            Children.Add(GetPathButton(null, "BtnUp32", this.BtnUp_Click));
            Children.Add(GetPathButton(null, "BtnHome32", this.BtnHome_Click));

            if (Model.CurrentPath.Count > 0)
            {
                //var elt = GetImage("ImgFolder", 32);
                //elt.Margin = new Thickness(5, 0, 0, 0);
                //Children.Add(elt);

                var pathes = Model.CurrentPath.ToList();
                for (int i = 0; i < pathes.Count; i++)
                {
                    var child = pathes[i];
                    if (i > 0) Children.Add(GetImage("ImgSeparator", 8));

                    Children.Add(this.GetPathButton(child.Name, "BtnPath", this.PathButtonClick, child));
                }
            }
        }

        const int LineHeight=34;

        private Grid GetImage(string imgName, int maxSize)
        {
            var elt = new Grid();
            elt.Height = LineHeight;
            var img = new Image();
            img.MaxWidth = maxSize;
            img.Stretch = Stretch.Uniform;
            elt.Children.Add(img);
            img.Source = (BitmapSource)this.FindResource(imgName);
            return elt;
        }

        Button GetPathButton(string text, string stylename, RoutedEventHandler click, object ctx=null)
        {
            var btn = new Button();
            btn.Height = LineHeight;
            btn.Style = (Style)FindResource(stylename);
            btn.DataContext = ctx;   
            if (!string.IsNullOrEmpty(text))
                btn.Content = text;
            btn.Click += click;
            return btn;
        }

        void PathButtonClick(object sender, RoutedEventArgs e)
        {
            var directory = ((sender as Button).DataContext as TouchDirectory);
            Model.SelectedDirectory = directory;
        }        

        private bool _initialized = false;
        internal void InitEvents()
        {
            if (!_initialized && Model != null)
            {
                CurrentPath_CollectionChanged(null, null);
                Model.CurrentPath.CollectionChanged +=
                    new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                        CurrentPath_CollectionChanged);
                _initialized = true;
            }
        }

        private void BtnUp_Click(object sender, RoutedEventArgs e)
        {
            if (Model.CurrentPath.Count > 1)
            {
                Model.CurrentPath.RemoveAt(Model.CurrentPath.Count - 1);
                var dir = Model.CurrentPath[Model.CurrentPath.Count - 1];
                Model.CurrentPath.Remove(dir);
                Model.SelectedDirectory = dir;
            }
            else
            {
                Model.CurrentPath.Clear();
                Model.SelectedDirectory = null;
            }
        }

        private void BtnHome_Click(object sender, RoutedEventArgs e)
        {
            Model.SelectedDirectory = null;
        }
    }
}
